package com.gitee.sop.layui.adminserver.module.base.util;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * web工具
 * @author: huwei
 * @date: 2019/10/17 15:25
 * @version: 1.0.0
 */
public class WebUtil {

    public static String completeRequestUrl(HttpServletRequest request){
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(request.getRequestURL()).append("?").append(request.getQueryString());
        try {
            return URLDecoder.decode(stringBuffer.toString() ,"utf-8");
        } catch (UnsupportedEncodingException e) {
            return stringBuffer.toString();
        }
    }

    /**
     * 获取用户真实IP地址，不使用request.getRemoteAddr();的原因是有可能用户使用了代理软件方式避免真实IP地址,
     * 可是，如果通过了多级反向代理的话，X-Forwarded-For的值并不止一个，而是一串IP值，究竟哪个才是真正的用户端的真实IP呢？
     * 答案是取X-Forwarded-For中第一个非unknown的有效IP字符串。
     * 如：X-Forwarded-For：192.168.1.110, 192.168.1.120, 192.168.1.130,
     * 192.168.1.100
     * 用户真实IP为： 192.168.1.110
     * @param request
     * @return
     */
    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        int sybmolIndex = -1;
        if(ip != null && (sybmolIndex = ip.indexOf(",")) != -1) {
            ip = ip.substring(0, sybmolIndex);
        }
        if(ip != null && ip.length() > 15) {
            ip = ip.substring(0, 16);
        }
        return ip;
    }

    /**
     * 组装一个完整的url地址
     * @param request
     * @param uri
     * @return
     */
    public static String newUrl(HttpServletRequest request, String uri) {
        return rootUrl(request).concat(uri);
    }

    /**
     * 获取系统的根url请求地址
     * @param request 请求
     * @return 根url请求地址
     */
    public static String rootUrl(HttpServletRequest request){
        String url = request.getRequestURL().toString();
        String uri = request.getRequestURI();
        return url.substring(0 ,url.indexOf(uri)).concat(request.getContextPath());
    }

    public static Map<String, String> getHeaderMap(HttpServletRequest request) {
        Map<String ,String> headerMap = new HashMap<>(16);
        Enumeration<String> headerNames =  request.getHeaderNames();
        while (headerNames.hasMoreElements()){
            String headerName = headerNames.nextElement();
            headerMap.put(headerName ,request.getHeader(headerName));
        }
        return headerMap;
    }
}
